package server;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 封装Response对象，需要依赖于OutputStream
 *
 * 该对象需要提供核心方法，输出html
 */
public class Response {

    private OutputStream outputStream;

    public Response() {
    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    // 使用输出流输出指定字符串
    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());
    }

    /**
     *
     * @param path  url，随后要根据url来获取到静态资源的绝对路径，进一步根据绝对路径读取该静态资源文件，最终通过
     *                  输出流输出
     *              /-----> classes
     *              /E:/learn/lagou/stage2/module1/TomcatAndNginx/code/Minicat/target/classes/E:\learn\lagou\stage2\module1\TomcatAndNginx\code\Minicat\webapps\demo1\index.html
     *              E:\learn\lagou\stage2\module1\TomcatAndNginx\code\Minicat\webapps\demo1\index.html
     */
    public void outputHtml(String path) throws IOException {
        // 获取静态资源文件的绝对路径
        String absoluteResourcePath = StaticResourceUtil.getAbsolutePath(path);
        // 输入静态资源文件
        File file = new File(absoluteResourcePath);
        if(file.exists() && file.isFile()) {
            // 读取静态资源文件，输出静态资源
            StaticResourceUtil.outputStaticResource(new FileInputStream(file),outputStream);
        }else{
            // 输出404
            output(HttpProtocolUtil.getHttpHeader404());
        }
    }

    private static List<String> staticList = new ArrayList();

    static {
        staticList.add(".html");
        staticList.add(".png");
        staticList.add(".js");
        staticList.add(".css");
        staticList.add(".svg");
        staticList.add(".ico");
        staticList.add(".jpg");
    }

    /**
     * 静态资源要以.html,.png,.js,.css,.svg,.ico,.jpg
     * @param realFilePath
     * @throws IOException
     */
    public void outputStatic(String realFilePath) throws IOException {
        String fileName = realFilePath.substring(realFilePath.lastIndexOf("."));
        if (staticList.contains(fileName)) {
            File file = new File(realFilePath);
            StaticResourceUtil.outputStaticResource(new FileInputStream(file), outputStream);
        }else {
            // 不是静态资源,输出404
            output(HttpProtocolUtil.getHttpHeader404());
        }
    }
}